package com.beiye.drivertransport.safelearn;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.TakePhotoScrollsActivity;
import com.beiye.drivertransport.SubActivity.TakefacePhotoScrollsActivity;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.DaiKaoCourseBean;
import com.beiye.drivertransport.bean.DaiKaoSureBean;
import com.beiye.drivertransport.bean.DaikaoAndHavetestCourseBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.MineUseBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.examination.ExaminationActivity;
import com.beiye.drivertransport.examination.adapter.TopicAdapter;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.HelpUtil;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.MessageEventTest;
import com.beiye.drivertransport.utils.MessageEventTest1;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.view.LinePathView;
import com.example.mylibrary.paper.view.QuestionViewPager;
import com.example.mylibrary.paper.view.SlidingUpPanelLayout;
import com.githang.statusbar.StatusBarCompat;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;


import es.dmoral.toasty.Toasty;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//日常培训考试
public class DailyTrainingExaminationActivity extends TwoBaseAty implements View.OnClickListener, SurfaceHolder.Callback {

    TextView tv_qpName;//试卷名称

    LinearLayout le_course;

    TextView tv_qptName;//试卷种类

    TextView tv_process;//答题进度

    TextView tv_overtime;//剩余时间

    TextView tv_sonce;//总分/及格分

    TextView tv_examiantion;//展开照片


    SurfaceView surfaceSv;//预览照片

            ImageView id_show_camera_iv;

            ImageView id_show_camera_iv1;

            ImageView id_show_camera_iv2;
    public ArrayList<DaikaoAndHavetestCourseBean.RowsBean> testlist = new ArrayList<>();

    ArrayList<DaikaoAndHavetestCourseBean.RowsBean> numlist = new ArrayList<>();
    final File fileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "Signname");


    private int sn;
    private int qpsn;
    private SlidingUpPanelLayout mLayout;
    private RecyclerView recyclerView;
    private TopicAdapter topicAdapter;
    private ImageView shadowView;
    private QuestionViewPager questionViewPager;
    private TimerTask timerTask;
    private Timer timer1;
    //  private int firsttime1;
    private int firsttime;
    private PopupWindow mUserPopWindow;
    private boolean isloob = true;
    private TextView tv_usename;
    private TextView tv_phone;
    private TextView tv_id;
    private PopupWindow mUserOnePopWindow;
    private int subformatTurnSecond;
    ArrayList<Integer> snlist = new ArrayList<>();
    private int formattimecnt;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            if (what == 0) {
                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                        Context.MODE_PRIVATE);
                int qpLength = sp.getInt("qpLength", 0);
                String format = sp.getString("format", "");
                if (format.equals("")) {
                    timerTask = new TimerTask() {
                        SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                                Context.MODE_PRIVATE);
                        int qpLengthtime = qpLength * 60;

                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    String firsttimestr1 = getStringTime1(firsttime);
                                    int remainingtime = qpLengthtime - firsttime;
                                    String firstandtime1 = getStringTime1(remainingtime);
                                    String stringTime1 = getStringTime1(qpLengthtime--);
                                    String s1 = stringTime1.split(":")[0];
                                    String s2 = stringTime1.split(":")[1];
                                    String s3 = stringTime1.split(":")[2];
                                    tv_overtime.setText(s1 + "小时" + s2 + "分" + s3 + "秒");
                                    if (stringTime1.equals(firstandtime1)) {
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putString("format", stringTime1);
                                        edit.putString("time", firsttimestr1);
                                        edit.commit();
                                        timerTask.cancel();
                                        if (!DailyTrainingExaminationActivity.this.isFinishing()) {
                                            showUserfirstPopwindow(firsttimestr1);
                                        }

                                    }
                                    LogUtils.e("format", stringTime1);
                                    LogUtils.e("firsttime1", firstandtime1);

                                }
                            });
                        }
                    };
                    timer1.schedule(timerTask, 0, 1000);
                } else {
                    String[] splitformat = format.split(":");
                    String format1 = splitformat[0];
                    String format2 = splitformat[1];
                    String format3 = splitformat[2];
                    int iformat1 = Integer.valueOf(format1);//转int
                    int iformat2 = Integer.valueOf(format2);//转int
                    int iformat3 = Integer.valueOf(format3);//转int
                    int formattime = iformat1 * 3600 + iformat2 * 60 + iformat3 * 1;
                    formattimecnt = formattime - 1;
                    timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    String stringTime1 = getStringTime1(formattimecnt--);
                                    String s1 = stringTime1.split(":")[0];
                                    String s2 = stringTime1.split(":")[1];
                                    String s3 = stringTime1.split(":")[2];
                                    tv_overtime.setText(s1 + "小时" + s2 + "分" + s3 + "秒");
                                    int qpLength = sp.getInt("qpLength", 0);
                                    int qpLengthtime = qpLength * 60;
                                    String[] splitformat = stringTime1.split(":");
                                    String format1 = splitformat[0];
                                    String format2 = splitformat[1];
                                    String format3 = splitformat[2];
                                    int iformat1 = Integer.valueOf(format1);//转int
                                    int iformat2 = Integer.valueOf(format2);//转int
                                    int iformat3 = Integer.valueOf(format3);//转int
                                    int formattime = iformat1 * 3600 + iformat2 * 60 + iformat3 * 1;
                                    int remainingtime1 = qpLengthtime - formattime;
                                    if (qpLengthtime == remainingtime1) {
                                        timerTask.cancel();
                                        showUserSignPopwindow();
                                    }


                                }
                            });
                        }
                    };
                    timer1.schedule(timerTask, 0, 1000);
                }
            }
        }
    };
    // private PopupWindow mUserSecondPopWindow;
    private int subformatTurnThird;
    private int subformatTurnFour;
    private TextView bt_next;
    private List snlist1;
    private PopupWindow msignPopwindow;
    private LinePathView signatureview;
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private ProgressDialog mProgressDialog;
    private int learnsn;
    private int subformatTurnthird;

    @Override
    public int getLayoutId() {
        return R.layout.activity_daily_training_examination;

    }

    private String getStringTime1(int cnt) {
        int hour = cnt / 3600;
        int min = cnt % 3600 / 60;
        int second = cnt % 60;
        return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
    }

    @Override
    public void initData() {
        tv_qpName = (TextView)findViewById(R.id.title1);
        le_course = (LinearLayout) findViewById(R.id.le_course);
        tv_qptName = (TextView)findViewById(R.id.tv_course);
        tv_process = (TextView)findViewById(R.id.tv_process);
        tv_overtime = (TextView)findViewById(R.id.tv_time3);
        tv_sonce = (TextView)findViewById(R.id.tv_sonce);
        tv_examiantion = (TextView)findViewById(R.id.img_examiantion);
        surfaceSv = (SurfaceView)findViewById(R.id.id_area_sv);
        id_show_camera_iv = (ImageView) findViewById(R.id.id_show_camera_iv);
        id_show_camera_iv1 = (ImageView) findViewById(R.id.id_show_camera_iv1);
        id_show_camera_iv2 = (ImageView) findViewById(R.id.id_show_camera_iv2);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        //注册EventBus
        EventBus.getDefault().register(this);
        timer1 = new Timer();
        firsttime = 0;
        initSlidingUoPanel();
        initList();

        findViewById(R.id.bt_pre).setOnClickListener(this);
        bt_next = (TextView) findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
        tv_examiantion.setOnClickListener(this);
        findViewById(R.id.img_examinationback).setOnClickListener(this);
        //摄像头
        int cameraCount = 1;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();// 得到摄像头的个数

        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);// 得到每一个摄像头的信息
            // 现在是后置，变更为前置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                /**
                 * 记得释放camera，方便其他应用调用
                 */
                releaseCamera();
                // 打开当前选中的摄像头
                mCamera = Camera.open(i);
                //   mCamera.setDisplayOrientation(90);//旋转度数
                // 通过surfaceview显示取景画面
                setStartPreview(mCamera, mHolder);

            }
        }
        // 获得句柄
        mHolder = surfaceSv.getHolder();
        // 添加回调
        mHolder.addCallback(this);
    }

    private void initSlidingUoPanel() {
        mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
        int height = getWindowManager().getDefaultDisplay().getHeight();

        LinearLayout dragView = (LinearLayout) findViewById(R.id.dragView);
        SlidingUpPanelLayout.LayoutParams params = new SlidingUpPanelLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (height * 0.5f));
        dragView.setLayoutParams(params);


        mLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {

            }

            @Override
            public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {
                if (newState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
                    MoveToPosition();
                }
            }
        });
        mLayout.setFadeOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
            }
        });
    }

    /**
     * 滚动 recycleView 到当前选择的位置
     */
    private void MoveToPosition() {
        gridLayoutManager.scrollToPositionWithOffset(curPosition2, 0);
    }

    private int prePosition;
    private int curPosition;
    GridLayoutManager gridLayoutManager;

    private void initList() {
        recyclerView = (RecyclerView) findViewById(R.id.list);

        gridLayoutManager = new GridLayoutManager(this, 8);

        topicAdapter = new TopicAdapter(this);

        recyclerView.setLayoutManager(gridLayoutManager);

        recyclerView.setAdapter(topicAdapter);


        topicAdapter.setOnTopicClickListener(new TopicAdapter.OnTopicClickListener() {
            @Override
            public void onClick(TopicAdapter.TopicViewHolder holder, int position) {
                curPosition = position;
                if (mLayout != null &&
                        (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
                    mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                }

                if (position == testlist.size() - 1) {
                    bt_next.setText("提交考卷");
                } else {
                    bt_next.setText("确认答案，下一题");
                }

                questionViewPager.setCurrentItem(position);

                topicAdapter.notifyCurPosition(curPosition);
                topicAdapter.notifyPrePosition(prePosition);

                prePosition = curPosition;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        SharedPreferences sp1 = getSharedPreferences("DailyTrainingExaminationActivity",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp1.edit();
        edit.clear();
        edit.commit();
        SharedPreferences sp = getSharedPreferences("score",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit2 = sp.edit();
        edit2.clear();
        edit2.commit();
        SharedPreferences sp2 = getSharedPreferences("opition",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit1 = sp2.edit();
        edit1.clear();
        edit1.commit();
        EventBus.getDefault().unregister(this);//反注册EventBus
        SharedPreferences sp3 = getSharedPreferences("CameraSwitchView",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit3 = sp3.edit();
        edit3.clear();
        edit3.commit();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEventTest1 event) {
        int tag = event.getTag();
        if (tag == 1) {
            int sn = event.getSn();
            String checkboxopition = event.getCheckboxopition();
            String radioopition = event.getRadioopition();
            SharedPreferences sp = getSharedPreferences("opition",
                    Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putInt("sn", sn);
            edit.putString("checkboxopition" + sn, checkboxopition);
            edit.putString("radioopition" + sn, radioopition);
            edit.commit();
        } else {
            String messageurl = event.getMessageurl();
            boolean isphoto = event.isIsphoto();
            int code = event.getCode();
            int faceRecgMark = event.getFaceRecgMark();
            String msg = event.getMsg();
            if (faceRecgMark == 1 || faceRecgMark == 2) {
                if (isphoto) {
                    Toasty.success(this, "人脸识别成功！", Toast.LENGTH_SHORT, true).show();
                    if (id_show_camera_iv.getDrawable() == null) {
                        id_show_camera_iv.setWillNotDraw(false);
                        id_show_camera_iv.setContentDescription(messageurl);
                        Bitmap bitmapByUrl = HelpUtil.getBitmapByUrl(messageurl);
                        id_show_camera_iv.setImageBitmap(bitmapByUrl);
                    }
                } else {
                    if (id_show_camera_iv.getDrawable() == null) {
                        id_show_camera_iv.setWillNotDraw(true);

                    }
                    if (code == 1) {
                        timerTask.cancel();
                        timer1.cancel();
                        timerTask = null;
                        timer1 = null;
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        View view = View
                                .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_layout, null);
                        TextView tv_message = (TextView) view
                                .findViewById(R.id.message);//输入内容
                        tv_message.setText("非常抱歉，您还没有上传个人头像，我们无法对您对你进行人脸识别。");
                        TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                        TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                        btn_cancel.setVisibility(View.GONE);
                        btn_sure.setText("确定");
                        final AlertDialog dialog = builder.create();
                        dialog.show();
                        dialog.getWindow().setContentView(view);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.setCancelable(false);
                        btn_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                                        Context.MODE_PRIVATE);
                                SharedPreferences.Editor edit = sp.edit();
                                edit.clear();
                                edit.commit();
                                finish();
                            }
                        });
                    } else if (code == 2) {
                        timerTask.cancel();
                        timer1.cancel();
                        timerTask = null;
                        timer1 = null;
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        View view = View
                                .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_layout, null);
                        TextView tv_message = (TextView) view
                                .findViewById(R.id.message);//输入内容
                        tv_message.setText("非常抱歉。系统正在进行维护，请您稍后再进行学习。");
                        TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                        TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                        btn_cancel.setVisibility(View.GONE);
                        btn_sure.setText("确定");
                        final AlertDialog dialog = builder.create();
                        dialog.show();
                        dialog.getWindow().setContentView(view);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.setCancelable(false);
                        btn_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                                        Context.MODE_PRIVATE);
                                SharedPreferences.Editor edit = sp.edit();
                                edit.clear();
                                edit.commit();
                                finish();
                            }
                        });
                    } else {
                        timerTask.cancel();
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        View view = View
                                .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_person_layout, null);
                        TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                        TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                        TextView btn_title = (TextView) view.findViewById(R.id.title);
                        TextView btn_message = (TextView) view.findViewById(R.id.message);
                        btn_title.setVisibility(View.VISIBLE);
                        btn_cancel.setText("我的头像");
                        btn_message.setText(msg);
                        final AlertDialog dialog = builder.create();
                        dialog.show();
                        dialog.getWindow().setContentView(view);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.setCancelable(false);
                        btn_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                                        Context.MODE_PRIVATE);
                                String time = sp.getString("time", "");
                                String photoUrl = sp.getString("photoUrl", "");
                                int faceRecgMark = sp.getInt("faceRecgMark", 0);
                                String firsttimesub = time.substring(3, 8);//截取字符串00:00:00变成00:00
                                Bundle bundle = new Bundle();
                                bundle.putInt("daikaosn", sn);
                                bundle.putString("snapTime", firsttimesub);
                                bundle.putInt("faceRecgMark", faceRecgMark);
                                bundle.putString("personphotoUrl", photoUrl);
                                startActivityForResult(TakefacePhotoScrollsActivity.class, bundle, 2);

                            }
                        });

                    }
                }

            } else if (faceRecgMark == 0) {
                if (isphoto) {
                    if (id_show_camera_iv.getDrawable() == null) {
                        id_show_camera_iv.setWillNotDraw(false);
                        id_show_camera_iv.setContentDescription(messageurl);
                        Bitmap bitmapByUrl = HelpUtil.getBitmapByUrl(messageurl);
                        id_show_camera_iv.setImageBitmap(bitmapByUrl);
                    }
                } else {
                    showToast("上传失败");
                    if (id_show_camera_iv.getDrawable() == null) {
                        id_show_camera_iv.setWillNotDraw(true);
                    }

                }

            }
        }
    }

    @Override
    public void onBackPressed() {

        TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
        builder.setMessage("是否离开本次考试");
        builder.setTitle("提示:");
        builder.setPositiveButton("离开", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                timerTask.cancel();
                timer1.cancel();
                timerTask = null;
                timer1 = null;
                finish();
                //设置你的操作事项
            }
        });

        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });


        builder.create().show();

        if (mLayout != null &&
                (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
            mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
        } else {
            //   super.onBackPressed();
        }
    }

    /**
     * 上一题
     */
    private synchronized void preQuestion() {
        int currentItem = questionViewPager.getCurrentItem();
        currentItem = currentItem - 1;
        if (currentItem > testlist.size() - 1) {
            currentItem = testlist.size() - 1;
        }
        questionViewPager.setCurrentItem(currentItem);
    }

    /**
     * 下一题
     */
    private synchronized void nextQuestion() {
        int currentItem = questionViewPager.getCurrentItem();
        currentItem = currentItem + 1;
        if (currentItem < 0) {
            currentItem = 0;
        }
        questionViewPager.setCurrentItem(currentItem);
    }


    public  void onClick(View v) {
        int id = v.getId();
        if (id == R.id.img_examiantion) {
            if (!isloob) {
                tv_examiantion.setText("展开照片");
                le_course.setVisibility(View.GONE);
                isloob = true;
            } else {
                tv_examiantion.setText("收起照片");
                le_course.setVisibility(View.VISIBLE);
                isloob = false;
            }
        } else if (id == R.id.bt_pre) {
            preQuestion();
            bt_next.setText("确认答案，下一题");
        } else if (id == R.id.bt_next) {//点下一题答案
            SharedPreferences sp = getSharedPreferences("opition",
                    Context.MODE_PRIVATE);
            int qusn = sp.getInt("sn", 0);
            String checkboxopition = sp.getString("checkboxopition" + qusn, "");
            String radioopition = sp.getString("radioopition" + qusn, "");
            String strsure = bt_next.getText().toString().trim();
            if (strsure.equals("确认答案，下一题")) {
                if (!checkboxopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, sortString(checkboxopition), this, 3);//复选题
                } else if (!radioopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, radioopition, this, 3);//单选题
                } else {
                    TiShiDialog.Builder builder1 = new TiShiDialog.Builder(DailyTrainingExaminationActivity.this);
                    builder1.setMessage("请先答题");
                    builder1.setTitle("提示:");
                    builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //设置你的操作事项
                        }
                    });
                    builder1.create().show();
                    return;
                }
            } else if (strsure.equals("确认答案，最后一题")) {
                if (!checkboxopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, sortString(checkboxopition), this, 3);//复选题
                } else if (!radioopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, radioopition, this, 3);//单选题
                } else {
                    TiShiDialog.Builder builder1 = new TiShiDialog.Builder(DailyTrainingExaminationActivity.this);
                    builder1.setMessage("请先答题");
                    builder1.setTitle("提示:");
                    builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //设置你的操作事项
                        }
                    });
                    builder1.create().show();
                    return;
                }

            } else if (strsure.equals("提交考卷")) {
                if (!checkboxopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, sortString(checkboxopition), this, 3);//复选题
                } else if (!radioopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    new Login().getDaikaoQuetion(qusn, radioopition, this, 3);//单选题
                }
                Drawable id_show_camera_ivDrawable = id_show_camera_iv.getDrawable();
                if (id_show_camera_ivDrawable == null) {
                    timerTask.cancel();
                    SharedPreferences sp2 = getSharedPreferences("DailyTrainingExaminationActivity",
                            Context.MODE_PRIVATE);
                    String format4 = sp2.getString("format", "");
                    int qpLength1 = sp2.getInt("qpLength", 0);
                    int qpLengthtime1 = qpLength1 * 60;
                    String[] splitformat1 = format4.split(":");
                    String format5 = splitformat1[0];
                    String format6 = splitformat1[1];
                    String format7 = splitformat1[2];
                    int iformat4 = Integer.valueOf(format5);//转int
                    int iformat5 = Integer.valueOf(format6);//转int
                    int iformat6 = Integer.valueOf(format7);//转int
                    int formattime1 = iformat4 * 3600 + iformat5 * 60 + iformat6 * 1;
                    int romaintime1 = qpLengthtime1 - formattime1;
                    String romaintimestr = getStringTime1(romaintime1);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("time", romaintimestr);
                    edit.commit();
                    showUseronePopwindow(romaintimestr);
                } else {
                    showUserSignPopwindow();
                }

//                    SharedPreferences sp1 = getSharedPreferences("DailyTrainingExaminationActivity",
//                            Context.MODE_PRIVATE);
//                    String format = sp1.getString("format", "");
//                    int qpLength = sp1.getInt("qpLength", 0);
//                    int qpLengthtime = qpLength * 60;
//                    String[] splitformat = format.split(":");
//                    String format1 = splitformat[0];
//                    String format2 = splitformat[1];
//                    String format3 = splitformat[2];
//                    int iformat1 = Integer.valueOf(format1);//转int
//                    int iformat2 = Integer.valueOf(format2);//转int
//                    int iformat3 = Integer.valueOf(format3);//转int
//                    int formattime = iformat1 * 3600 + iformat2 * 60 + iformat3 * 1;
//                    int   romaintime=qpLengthtime-formattime;
//                    if(romaintime<=firsttime1)//第二次拍照后就可以提交第三张照片
//                    {
//                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(this);
//                        builder1.setMessage("还未到交卷时间，请检查您的答案无误之后再提交。");
//                        builder1.setTitle("提示:");
//                        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.dismiss();
//                                //设置你的操作事项
//                            }
//                        });
//
//                        builder1.create().show();
//                        return;
//                    }
//                    Drawable id_show_camera_iv2Drawable = id_show_camera_iv2.getDrawable();
//                    if(id_show_camera_iv2Drawable==null)
//                    {
//                        timerTask.cancel();
//                        SharedPreferences sp2 = getSharedPreferences("DailyTrainingExaminationActivity",
//                                Context.MODE_PRIVATE);
//                        String format4 = sp2.getString("format", "");
//                        int qpLength1 = sp2.getInt("qpLength", 0);
//                        int qpLengthtime1 = qpLength1 * 60;
//                        String[] splitformat1 = format4.split(":");
//                        String format5 = splitformat1[0];
//                        String format6 = splitformat1[1];
//                        String format7 = splitformat1[2];
//                        int iformat4 = Integer.valueOf(format5);//转int
//                        int iformat5 = Integer.valueOf(format6);//转int
//                        int iformat6 = Integer.valueOf(format7);//转int
//                        int formattime1 = iformat4 * 3600 + iformat5 * 60 + iformat6 * 1;
//                        int   romaintime1=qpLengthtime1-formattime1;
//                        String romaintimestr = getStringTime1(romaintime1);
//                        showUserthreePopwindow(romaintimestr);
//                    }else {
//                        showUserSignPopwindow();
//
//                    }
            }
        } else if (id == R.id.img_examinationback) {
            TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
            builder.setMessage("是否离开本次考试");
            builder.setTitle("提示:");
            builder.setPositiveButton("离开", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    timerTask.cancel();
                    timer1.cancel();
                    timerTask = null;
                    timer1 = null;
                    finish();
                    //设置你的操作事项
                }
            });

            builder.setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });


            builder.create().show();
        }
    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        //通过name关键字取出用户名
        sn = bundle.getInt("daikaosn");
        // String   qptId = bundle.getString("qptId");
        qpsn = bundle.getInt("daikaoqpsn");
        learnsn = bundle.getInt("learnsn");
        int faceRecgMark = bundle.getInt("faceRecgMark");
        String photoUrl = bundle.getString("photoUrl");
        SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("photoUrl", photoUrl);
        edit.putInt("faceRecgMark", faceRecgMark);
        edit.commit();

        OkGo.get(AppInterfaceConfig.BASE_URL + "course/paper/" + qpsn)     // 请求方式和请求url
                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String result, Call call, Response response) {
                        DaiKaoCourseBean daiKaoCourseBean = JSON.parseObject(result, DaiKaoCourseBean.class);
                        DaiKaoCourseBean.DataBean data = daiKaoCourseBean.getData();

                        if (data == null) {
                            return;
                        }

                        String qptName = data.getQptName();//试卷种类名称

                        String qpName = data.getQpName();//试卷名称

                        int questionNo = data.getQuestionNo();//测验题数
                        int totalScore = data.getTotalScore();//总分
                        int passScore = data.getPassScore();//及格分
                        //试卷时长
                        int qpLength = data.getQpLength();
                        SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putInt("qpLength", qpLength);
                        edit.commit();
                        tv_qptName.setText(qptName);
                        tv_qpName.setText(qpName);
                        tv_process.setText("0/" + questionNo);
                        tv_sonce.setText(totalScore + "分  （" + passScore + "分及格)");

                        Thread thread1 = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                //在子线程中执行任务，执行完成或需要通知UI线程时调用以下方法
                                mHandler.sendEmptyMessage(0);
                            }
                        });
                        thread1.start();

                    }
                });

        //待考试卷详情
        new Login().getDaikao(sn, 1, 10000, this, 1);
    }

    //个人信息弹窗展示
    private void showUserfirstPopwindow(final String stringTime) {
        //    final  String firsttimesub = stringTime.substring(3, 8);//截取字符串00:00:00变成00:00
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        new Login().getUser(userId, this, 4);
        //设置contentView
        View contentView = LayoutInflater.from(DailyTrainingExaminationActivity.this).inflate(R.layout.user_popwindowlayout1, null);
        mUserPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);

        final LinearLayout le_takepicture = (LinearLayout) contentView.findViewById(R.id.le_takepicture);//跟布局
        TextView img_sure = (TextView) contentView.findViewById(R.id.img_photo1);//确定
        TextView img_no = (TextView) contentView.findViewById(R.id.img_photo2);//取消
        // tv_company = (TextView) contentView.findViewById(R.id.tv_photo8);
        tv_usename = (TextView) contentView.findViewById(R.id.tv_photo9);
        tv_phone = (TextView) contentView.findViewById(R.id.tv_photo10);
        tv_id = (TextView) contentView.findViewById(R.id.tv_photo11);
        mUserPopWindow.setFocusable(false);
        mUserPopWindow.setOutsideTouchable(false);
        mUserPopWindow.showAtLocation(le_takepicture, Gravity.CENTER, 0, 0);

        img_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mUserPopWindow.dismiss();
                showUseronePopwindow(stringTime);
            }
        });
        img_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPopWindow.dismiss();
                timerTask.cancel();
                timer1.cancel();
                timerTask = null;
                timer1 = null;
                finish();
            }
        });
    }

    private void showUseronePopwindow(final String stringTime) {
        //设置contentView
        View contentView = LayoutInflater.from(DailyTrainingExaminationActivity.this).inflate(R.layout.test_popwindowlayout, null);
        mUserOnePopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        ImageView takepciture = (ImageView) contentView.findViewById(R.id.img_photo1);//点拍照
        final LinearLayout le_takepicture = (LinearLayout) contentView.findViewById(R.id.le_takepicture1);//跟布局
        mUserOnePopWindow.setFocusable(false);
        mUserOnePopWindow.setOutsideTouchable(false);
        mUserOnePopWindow.showAtLocation(le_takepicture, Gravity.CENTER, 0, 0);


        takepciture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mUserOnePopWindow.dismiss();
                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                        Context.MODE_PRIVATE);
                String photoUrl = sp.getString("photoUrl", "");
                int faceRecgMark = sp.getInt("faceRecgMark", 0);
                String firsttimesub = stringTime.substring(3, 8);//截取字符串00:00:00变成00:00
                Bundle bundle = new Bundle();
                bundle.putInt("daikaosn", sn);
                bundle.putString("snapTime", firsttimesub);
                bundle.putInt("faceRecgMark", faceRecgMark);
                bundle.putString("personphotoUrl", photoUrl);
                LogUtils.e("daikaosnsnapTime", sn + "" + firsttimesub);
                startActivityForResult(TakefacePhotoScrollsActivity.class, bundle, 0);

            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        if (mUserPopWindow != null && mUserPopWindow.isShowing()) {
            return false;
        } else if (mUserOnePopWindow != null && mUserOnePopWindow.isShowing()) {
            return false;
        } else if (msignPopwindow != null && msignPopwindow.isShowing()) {
            return false;
        }

        return super.dispatchTouchEvent(event);
    }

    /*****
     * 接受第二个Activity返回的数据时调用
     *
     * ****/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data == null) {
            return;
        }
        String url = data.getStringExtra("url");
        // 根据返回码的不同，设置参数
        if (requestCode == 0) {
            if (resultCode == 1) {
                if (url != null && !TextUtils.isEmpty(url)) {
                    // le_course.setVisibility(View.VISIBLE);
//                    id_show_camera_iv.setContentDescription(url);
//                    Bitmap bitmapByUrl = HelpUtil.getBitmapByUrl(url);
//                    id_show_camera_iv.setImageBitmap(bitmapByUrl);
                    SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                            Context.MODE_PRIVATE);
                    String stringTime = sp.getString("format", "");
                    int index1 = stringTime.indexOf(":");
                    int index2 = stringTime.indexOf(":", index1 + 1);
                    int hh = Integer.parseInt(stringTime.substring(0, index1));
                    int mi = Integer.parseInt(stringTime.substring(index1 + 1, index2));
                    int ss = Integer.parseInt(stringTime.substring(index2 + 1));
                    int formatTurnSecond = hh * 60 * 60 + mi * 60 + ss;
                    subformatTurnSecond = formatTurnSecond - 1;
                    String stringTime1 = getStringTime1(subformatTurnSecond);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("format", stringTime1);
                    edit.commit();
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //在子线程中执行任务，执行完成或需要通知UI线程时调用以下方法
                            mHandler.sendEmptyMessage(0);
                        }
                    });
                    thread.start();

//                    timerTask = new TimerTask() {
//                        @Override
//                        public void run() {
//                            runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
//                                            Context.MODE_PRIVATE);
//                                    int qpLength = sp.getInt("qpLength", 0);
//                                    int qpLengthtime = qpLength * 60;
//                                    String stringTime = getStringTime1(subformatTurnSecond--);
//                                    String s1 = stringTime.split(":")[0];
//                                    String s2 = stringTime.split(":")[1];
//                                    String s3 = stringTime.split(":")[2];
//                                    tv_overtime.setText(s1+"小时"+s2+"分"+s3+"秒");
//                                    int index1 = stringTime.indexOf(":");
//                                    int index2 = stringTime.indexOf(":", index1 + 1);
//                                    int hh = Integer.parseInt(stringTime.substring(0, index1));
//                                    int mi = Integer.parseInt(stringTime.substring(index1 + 1, index2));
//                                    int ss = Integer.parseInt(stringTime.substring(index2 + 1));
//                                    int time = hh * 60 * 60 + mi * 60 + ss;
//                                    int remainingtime = qpLengthtime - time;
//                                    String firstandtimestr = getStringTime1(remainingtime);
//                                    SharedPreferences.Editor edit = sp.edit();
//                                    edit.putString("format", stringTime);
//                                    edit.commit();
//                                    LogUtils.e("format", stringTime);
//                                    String qpLengthtimestr = getStringTime1(qpLengthtime);
//                                    if(firstandtimestr.equals(qpLengthtimestr))
//                                    {
//
//                                        showUserSignPopwindow();
//
//                                    }
//
//                                }
//                            });
//                        }
//                    };
//                    timer1.schedule(timerTask, 0, 1000);
                }
            }
        }
        if (requestCode == 2) {//人脸成功返回
            if (resultCode == 1) {

                SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                        Context.MODE_PRIVATE);
                String stringTime = sp.getString("format", "");
                int index1 = stringTime.indexOf(":");
                int index2 = stringTime.indexOf(":", index1 + 1);
                int hh = Integer.parseInt(stringTime.substring(0, index1));
                int mi = Integer.parseInt(stringTime.substring(index1 + 1, index2));
                int ss = Integer.parseInt(stringTime.substring(index2 + 1));
                int formatTurnSecond = hh * 60 * 60 + mi * 60 + ss;
                subformatTurnthird = formatTurnSecond;
                String stringTime1 = getStringTime1(subformatTurnthird);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("format", stringTime1);
                edit.commit();
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //在子线程中执行任务，执行完成或需要通知UI线程时调用以下方法
                        mHandler.sendEmptyMessage(0);
                    }
                });
                thread.start();
//                            int qpLength = sp.getInt("qpLength", 0);
//                            int qpLengthtime = qpLength * 60;
//                            int remontime1 = qpLengthtime - subformatTurnFour;
//                            if(qpLengthtime==remontime1)
//                            {
//                                showUserSignPopwindow();
//                                SharedPreferences sp1 = getSharedPreferences("ExaminationActivity",
//                                        Context.MODE_PRIVATE);
//                                SharedPreferences.Editor edit = sp1.edit();
//                                edit.putString("format", stringTime);
//                                edit.commit();
//                            } else {
//                                showUserSignPopwindow();
//
//                            }
            }
        }
    }

    //弹窗签名
    @SuppressLint("HandlerLeak")
    private void showUserSignPopwindow() {

        timerTask.cancel();
        View contentView = LayoutInflater.from(DailyTrainingExaminationActivity.this).inflate(R.layout.sign_popwindowlayout, null);
        msignPopwindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        LinearLayout le_takepicture = (LinearLayout) contentView.findViewById(R.id.le_takepicture);//
        msignPopwindow.setFocusable(false);
        msignPopwindow.setOutsideTouchable(false);
        msignPopwindow.showAtLocation(le_takepicture, Gravity.CENTER, 0, 0);

        //签名
        signatureview = (LinePathView) contentView.findViewById(R.id.signatureview);
        //重签
        TextView tv_resign = (TextView) contentView.findViewById(R.id.tv_violation1);
        TextView tv_sure = (TextView) contentView.findViewById(R.id.tv_sure);//确定

        tv_resign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signatureview.clear();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(DailyTrainingExaminationActivity.this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        ToastUtil.showShortToast(DailyTrainingExaminationActivity.this, "请到手机设置界面里找驾运宝允许开启读写手机存储,否则不能点确定");
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                save(fileDir);

            }
        });


    }

    /**
     * 得到图片的路径 以及图片的名字
     */
    private String getFilePath(File fileDir) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        final String fileName = simpleDateFormat.format(new Date()) + ".png";
        LogUtils.e("filename", "---" + fileName);
        File file = new File(fileDir, fileName);
        return file.getAbsolutePath();
    }

    /**
     * 保存图片
     */
    private void save(File fileDir) {
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        final String fileName = simpleDateFormat.format(new Date()) + ".png";
        File savefile = new File(fileDir, fileName);
        final String filePath = getFilePath(fileDir);
        if (!signatureview.getTouched()) //是否签名
        {
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(DailyTrainingExaminationActivity.this);
            builder1.setMessage("请完善您的签名");
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();
            return;
        }
        msignPopwindow.dismiss();
        try {
            signatureview.save(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadImg(savefile);//上传服务器封装方法

    }

    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private final OkHttpClient client = new OkHttpClient();

    private void uploadImg(File mediaFile) {
        SharedPreferences sp = getSharedPreferences("DailyTrainingExaminationActivity",
                Context.MODE_PRIVATE);
        String orgId = sp.getString("orgId", "");
        // mImgUrls为存放图片的url集合
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

        if (mediaFile != null) {
            builder.addFormDataPart("psignImg", mediaFile.getName(), RequestBody.create(MEDIA_TYPE_PNG, mediaFile));
            builder.addFormDataPart("sn", sn + "");
            builder.addFormDataPart("qpSn", qpsn + "");
            builder.addFormDataPart("utchSn", learnsn + "");

        }
        MultipartBody requestBody = builder.build();
        mProgressDialog = new ProgressDialog(DailyTrainingExaminationActivity.this, ProgressDialog.THEME_HOLO_DARK);
        mProgressDialog.setMessage("提交中...");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        //构建请求
        Request request = new Request.Builder()
                .url(AppInterfaceConfig.BASE_URL + "app/paper/finishMtForT")//地址
                .post(requestBody)//添加请求体
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtils.e("上传失败", e.getLocalizedMessage());
                mProgressDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                LogUtils.e("上传成功", str);
                DaiKaoSureBean sureBean = JSON.parseObject(str, DaiKaoSureBean.class);
                DaiKaoSureBean.DataBean data = sureBean.getData();
                int passMark = data.getPassMark();
                float score = data.getScore();
                int code = sureBean.getCode();
                mProgressDialog.dismiss();
                timerTask.cancel();
                timer1.cancel();
                timerTask = null;
                timer1 = null;
                if (passMark == 1) {
                    Looper.prepare();
                    AlertDialog.Builder builder = new AlertDialog.Builder(DailyTrainingExaminationActivity.this);
                    View view = View
                            .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_layout, null);
                    TextView tv_message = (TextView) view
                            .findViewById(R.id.message);//输入内容
                    tv_message.setText("您此次考得" + score + "分及格");
                    TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                    TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                    btn_cancel.setVisibility(View.GONE);
                    btn_sure.setText("确定");
                    final AlertDialog dialog = builder.create();
                    dialog.show();
                    dialog.getWindow().setContentView(view);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.setCancelable(false);
                    btn_sure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                            SharedPreferences sp = getSharedPreferences("homefgt", Context.MODE_PRIVATE);
                            String orgName = sp.getString("orgName", "");
                            String orgId = sp.getString("orgId", "");
                            String forbidHour = sp.getString("forbidHour", "");
                            int minPer = sp.getInt("minPer", 0);
                            int readInterval = sp.getInt("readInterval", 0);
                            int Mark = sp.getInt("Mark", 0);
                            int signMark1 = sp.getInt("signMark", 0);
                            int photoMark = sp.getInt("photoMark", 0);
                            int faceRecgMark = sp.getInt("faceRecgMark", 0);
                            int feeType = sp.getInt("feeType", 0);
                            String photoUrl = sp.getString("personphotoUrl", "");
                            int mtSeqMark = sp.getInt("mtSeqMark", 0);
                            int allowMuT = sp.getInt("allowMuT", 0);
                            if (faceRecgMark == 1) {
                                Bundle bundle1 = new Bundle();
                                bundle1.putInt("minPer", minPer);
                                bundle1.putInt("readInterval", readInterval);
                                bundle1.putInt("signMark", signMark1);
                                bundle1.putInt("Mark", Mark);
                                bundle1.putString("orgId", orgId);
                                bundle1.putString("forbidHour", forbidHour);
                                bundle1.putInt("photoMark", photoMark);
                                bundle1.putInt("faceRecgMark", 1);
                                bundle1.putString("personphotoUrl", photoUrl);
                                bundle1.putInt("feeType", feeType);
                                bundle1.putString("orgName", orgName);
                                bundle1.putInt("mtSeqMark", mtSeqMark);
                                bundle1.putInt("allowMuT", allowMuT);
                                startActivity(SpecifiedLearningHomeActivity.class, bundle1);
                                finish();
                            } else if (faceRecgMark == 0) {
                                Bundle bundle1 = new Bundle();
                                bundle1.putInt("minPer", minPer);
                                bundle1.putInt("readInterval", readInterval);
                                bundle1.putInt("signMark", signMark1);
                                bundle1.putInt("Mark", Mark);
                                bundle1.putString("orgId", orgId);
                                bundle1.putString("forbidHour", forbidHour);
                                bundle1.putInt("photoMark", photoMark);
                                bundle1.putInt("faceRecgMark", 0);
                                bundle1.putString("personphotoUrl", "");
                                bundle1.putInt("feeType", feeType);
                                bundle1.putString("orgName", orgName);
                                bundle1.putInt("mtSeqMark", mtSeqMark);
                                bundle1.putInt("allowMuT", allowMuT);
                                startActivity(SpecifiedLearningHomeActivity.class, bundle1);
                                finish();
                            }

                        }
                    });

                    Looper.loop();
                } else {
                    Looper.prepare();
                    if (code == 9) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(DailyTrainingExaminationActivity.this);
                        View view = View
                                .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_layout, null);
                        TextView tv_message = (TextView) view
                                .findViewById(R.id.message);//输入内容
                        tv_message.setText("您此次考得" + score + "分不及格,您考试未合格，您本月需要重新学习！");
                        TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                        TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                        btn_cancel.setVisibility(View.GONE);
                        btn_sure.setText("确定");
                        final AlertDialog dialog = builder.create();
                        dialog.show();
                        dialog.getWindow().setContentView(view);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.setCancelable(false);
                        btn_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                SharedPreferences sp = getSharedPreferences("homefgt", Context.MODE_PRIVATE);
                                String orgName = sp.getString("orgName", "");
                                String orgId = sp.getString("orgId", "");
                                String forbidHour = sp.getString("forbidHour", "");
                                int minPer = sp.getInt("minPer", 0);
                                int readInterval = sp.getInt("readInterval", 0);
                                int Mark = sp.getInt("Mark", 0);
                                int signMark1 = sp.getInt("signMark", 0);
                                int photoMark = sp.getInt("photoMark", 0);
                                int faceRecgMark = sp.getInt("faceRecgMark", 0);
                                int feeType = sp.getInt("feeType", 0);
                                String photoUrl = sp.getString("personphotoUrl", "");
                                int mtSeqMark = sp.getInt("mtSeqMark", 0);
                                int allowMuT = sp.getInt("allowMuT", 0);
                                if (faceRecgMark == 1) {
                                    Bundle bundle1 = new Bundle();
                                    bundle1.putInt("minPer", minPer);
                                    bundle1.putInt("readInterval", readInterval);
                                    bundle1.putInt("signMark", signMark1);
                                    bundle1.putInt("Mark", Mark);
                                    bundle1.putString("orgId", orgId);
                                    bundle1.putString("forbidHour", forbidHour);
                                    bundle1.putInt("photoMark", photoMark);
                                    bundle1.putInt("faceRecgMark", 1);
                                    bundle1.putString("personphotoUrl", photoUrl);
                                    bundle1.putInt("feeType", feeType);
                                    bundle1.putString("orgName", orgName);
                                    bundle1.putInt("mtSeqMark", mtSeqMark);
                                    bundle1.putInt("allowMuT", allowMuT);
                                    startActivity(SpecifiedLearningHomeActivity.class, bundle1);
                                    finish();
                                } else if (faceRecgMark == 0) {
                                    Bundle bundle1 = new Bundle();
                                    bundle1.putInt("minPer", minPer);
                                    bundle1.putInt("readInterval", readInterval);
                                    bundle1.putInt("signMark", signMark1);
                                    bundle1.putInt("Mark", Mark);
                                    bundle1.putString("orgId", orgId);
                                    bundle1.putString("forbidHour", forbidHour);
                                    bundle1.putInt("photoMark", photoMark);
                                    bundle1.putInt("faceRecgMark", 0);
                                    bundle1.putString("personphotoUrl", "");
                                    bundle1.putInt("feeType", feeType);
                                    bundle1.putString("orgName", orgName);
                                    bundle1.putInt("mtSeqMark", mtSeqMark);
                                    bundle1.putInt("allowMuT", allowMuT);
                                    startActivity(SpecifiedLearningHomeActivity.class, bundle1);
                                    finish();
                                }
                            }
                        });

                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(DailyTrainingExaminationActivity.this);
                        View view = View
                                .inflate(DailyTrainingExaminationActivity.this, R.layout.diolog_learn_layout, null);
                        TextView tv_message = (TextView) view
                                .findViewById(R.id.message);//输入内容
                        tv_message.setText("您此次考得" + score + "分不及格");
                        TextView btn_sure = (TextView) view.findViewById(R.id.positiveButton);
                        TextView btn_cancel = (TextView) view.findViewById(R.id.negativeButton);
                        btn_cancel.setVisibility(View.GONE);
                        btn_sure.setText("确定");
                        final AlertDialog dialog = builder.create();
                        dialog.show();
                        dialog.getWindow().setContentView(view);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.setCancelable(false);
                        btn_sure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                finish();
                            }
                        });
                    }
                    Looper.loop();
                }

            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        /**
         * 记得释放camera，方便其他应用调用
         */
        releaseCamera();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (this.checkCameraHardware(this) && (mCamera == null)) {
            // 打开camera
            mCamera = getCamera();
            if (mHolder != null) {
                setStartPreview(mCamera, mHolder);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //摄像头
        int cameraCount = 1;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();// 得到摄像头的个数

        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);// 得到每一个摄像头的信息
            // 现在是后置，变更为前置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                /**
                 * 记得释放camera，方便其他应用调用
                 */
                releaseCamera();
                // 打开当前选中的摄像头
                mCamera = Camera.open(i);
                //   mCamera.setDisplayOrientation(90);//旋转度数
                // 通过surfaceview显示取景画面
                setStartPreview(mCamera, mHolder);

            }
        }


    }

    private Camera getCamera() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            camera = null;
            // LogUtils.e(TAG, "Camera is not available (in use or does not exist)");
        }
        return camera;
    }

    /**
     * 释放mCamera
     */
    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();// 停掉原来摄像头的预览
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        setStartPreview(mCamera, mHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        setStartPreview(mCamera, mHolder);

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 当surfaceview关闭时，关闭预览并释放资源
        /**
         * 记得释放camera，方便其他应用调用
         */
        releaseCamera();
        holder = null;
        surfaceSv = null;
    }

    /**
     * Check if this device has a camera
     */
    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    /**
     * 设置camera显示取景画面,并预览
     *
     * @param camera
     */
    private void setStartPreview(Camera camera, SurfaceHolder holder) {
        try {
            camera.setPreviewDisplay(holder);
            //开始预览
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            // Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            final DaikaoAndHavetestCourseBean courseBean = JSON.parseObject(result, DaikaoAndHavetestCourseBean.class);
            List<DaikaoAndHavetestCourseBean.RowsBean> rowslist = courseBean.getRows();
            testlist.clear();
            testlist.addAll(rowslist);
            for (int j = 0; j < testlist.size(); j++) {
                DaikaoAndHavetestCourseBean.RowsBean temp = testlist.get(j);
                temp.setSeqNo(j + 1);
                if (testlist.get(j).getQtype() == 3 || testlist.get(j).getQtype() == 1) {

                    if (testlist.get(j).getUserOption() == null) {
                        testlist.get(j).setQuestion_select(-1);
                    } else if (testlist.get(j).getUserOption().equals("A")) {
                        testlist.get(j).setQuestion_select(0);
                    } else if (testlist.get(j).getUserOption().equals("B")) {
                        testlist.get(j).setQuestion_select(1);
                    } else if (testlist.get(j).getUserOption().equals("C")) {
                        testlist.get(j).setQuestion_select(2);
                    } else if (testlist.get(j).getUserOption().equals("D")) {
                        testlist.get(j).setQuestion_select(3);
                    } else if (testlist.get(j).getUserOption().equals("E")) {
                        testlist.get(j).setQuestion_select(4);
                    } else if (testlist.get(j).getUserOption().equals("F")) {
                        testlist.get(j).setQuestion_select(5);
                    }
                } else if (testlist.get(j).getQtype() == 2) {
                    if (testlist.get(j).getUserOption() == null) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABC")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("DEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("AB")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AC")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("AE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("AF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("BC")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("CD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("CE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("CF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("DE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("DF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("EF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("A")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("B")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("C")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("D")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("E")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("F")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    }
                }

            }

            if (topicAdapter != null) {
                topicAdapter.setDataNum(testlist.size());
                topicAdapter.setDatas(testlist);

            }

            initReadViewPager(testlist);


        } else if (what == 3) {
            nextQuestion();
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            if (code == 0) {

                SharedPreferences sp = getSharedPreferences("opition",
                        Context.MODE_PRIVATE);
                int sn = sp.getInt("checksn", 0);

                snlist.add(sn);
                snlist1 = removeDuplicate(snlist);
                int num = snlist1.size();
                tv_process.setText(num + "/" + testlist.size());

                if (sn == testlist.get(testlist.size() - 1).getSn()) {
                    bt_next.setText("提交考卷");
                } else if (sn == testlist.get(testlist.size() - 2).getSn()) {
                    if (num != testlist.size()) {
                        bt_next.setText("确认答案，最后一题");
                    } else {
                        bt_next.setText("提交考卷");
                    }
                } else {
                    bt_next.setText("确认答案，下一题");
                }

                for (int i = 0; i < testlist.size(); i++) {
                    if (testlist.get(i).getSn() == sn) {
                        testlist.get(i).setSelect(true);
                    }

                }
                if (topicAdapter != null) {
                    topicAdapter.setDataNum(testlist.size());
                    topicAdapter.setDatas(testlist);

                }
            }
        } else if (what == 4) {
            MineUseBean mineUseBean = JSON.parseObject(result, MineUseBean.class);
            MineUseBean.DataBean data = mineUseBean.getData();
            String userName = data.getUserName();
            String userMobile = data.getUserMobile();

            if (userName == null) {
                tv_usename.setText("姓    名：");

            } else {
                tv_usename.setText("姓    名：" + userName);
            }
            if (userMobile == null) {
                tv_phone.setText("手    机：");
            } else {
                tv_phone.setText("手    机：" + userMobile);
            }


            String idcNo = data.getIdcNo();

            if (idcNo == null) {
                tv_id.setText("身份证：");
            } else {

                tv_id.setText("身份证：" + idcNo);

            }

            //  tv_company.setText("企    业："+orgName);
        }


        super.onSuccess(result, call, response, what);
    }

    //字母排序
    public String sortString(String str) {

        //  System.out.println("排序前的字符串是:" + str);
        LogUtils.e("排序前的字符串是:", str);
        // 排序前字符串转换为字符数组
        char[] cs = str.toCharArray();

        // 升序排序
        for (int i = 0; i < cs.length - 1; i++) {
            for (int j = 0; j < cs.length - 1 - i; j++) {

                char temp = 0;
                if (cs[j] > cs[j + 1]) {

                    temp = cs[j];
                    cs[j] = cs[j + 1];
                    cs[j + 1] = temp;
                }
            }
        }

        // 排序以后的字符串
        String newString = "";

        for (int i = 0; i < cs.length; i++) {
            newString += cs[i];
        }
        // 打印排序以后的字符串
        LogUtils.e("排序后的字符串是:", newString);
        return newString;

    }

    public static List removeDuplicate(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 3) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            //     int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
            builder.setMessage(msg);
            builder.setTitle("提示:");
            builder.setPositiveButton("我知道啦", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            builder.create().show();

        }

        super.onError(result, call, response, what);
    }

    private int prePosition2;
    private int curPosition2;

    private void initReadViewPager(final ArrayList<DaikaoAndHavetestCourseBean.RowsBean> testlist) {
        shadowView = (ImageView) findViewById(R.id.shadowView);
        questionViewPager = (QuestionViewPager) findViewById(R.id.readerViewPager);
        //  questionViewPager.setOffscreenPageLimit(1);
        questionViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                DaikaoAndHavetestCourseBean.RowsBean rowsBean = testlist.get(position);
                QuestionDailyTrainingExaminationFragment fragment = QuestionDailyTrainingExaminationFragment.newInstance(rowsBean, position);

                return fragment;
            }

            @Override
            public int getCount() {
                return testlist.size();
            }
        });
        questionViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                shadowView.setTranslationX(questionViewPager.getWidth() - positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {

                curPosition2 = position;
                topicAdapter.notifyCurPosition(curPosition2);
                topicAdapter.notifyPrePosition(prePosition2);

                prePosition2 = curPosition2;
                MoveToPosition();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
